package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.CategoryBean;
import com.cskaoyan.bean.vo.GoodsListBean;
import com.cskaoyan.bean.vo.goodsDetailNean.CommentBean;
import com.cskaoyan.bean.vo.goodsDetailNean.CommentBeanSon;
import com.cskaoyan.bean.vo.goodsDetailNean.GoodsDetailBean;
import com.cskaoyan.bean.vo.goodsDetailNean.SpecificationBean;
import com.cskaoyan.bean.vo.homeIndex.GrouponIndex;
import com.cskaoyan.bean.vo.homeIndex.NewGoods;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.mapper.FootPrintMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.IndexMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 杨杰
 * @version V1.0
 * @Package com.cskaoyan.service.wx
 * @date 2020/12/9 9:16
 * @Copyright © 暂时还没有
 * @description
 */
@Service
public class WxGoodsServiceImp implements WxGoodsSrvice{

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    IndexMapper indexMapper;
    @Autowired
    FootPrintMapper footPrintMapper;

    private final static String styleSpecification = "规格";
    private final static String colorSpecification = "颜色";

    @Override
    public Integer queryAllCount() {

        /*
        查询所有列数
         */
        long count = goodsMapper.countByExample(null);
        return (int)count;
    }

    @Override
    public CategoryBean getCategory(Integer id) {
        CategoryBean categoryBean = new CategoryBean();

        //0.确认是否为父类id，否则找到父类id
        Category categoryJudge = categoryMapper.selectByPrimaryKey(id);
        Category parentCategory = categoryJudge;
        Category currentCategory = null;
        System.out.println("level" + categoryJudge.getLevel());
        if (!"L1".equals(categoryJudge.getLevel())){
            //当id所在记录非L1，则currentCategory为当前id的记录
            currentCategory = categoryMapper.selectByPrimaryKey(id);
            id = categoryJudge.getPid();//纠正为当前类的父类id

            //1.id为L1，根据L1查询parentCategory
            parentCategory = categoryMapper.selectByPrimaryKey(id);

        }
        categoryBean.setParentCategory(parentCategory);


        //2. 根据L1 查询L2 brotherCategory
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();

        criteria.andPidEqualTo(id);
        List<Category> brotherCategory = categoryMapper.selectByExample(categoryExample);
        categoryBean.setBrotherCategory(brotherCategory);

        //3.currentCategory
        if (currentCategory == null){
            System.out.println(brotherCategory.get(0));
            currentCategory = brotherCategory.get(0);
        }
        categoryBean.setCurrentCategory(currentCategory);//取第一个为currentCategory
        return categoryBean;
    }

    @Override
    public GoodsListBean getListByCategoryId(Integer categoryId, Integer page, Integer size) {
        GoodsListBean goodsListBean = new GoodsListBean();
        //1.goods详情
        List<NewGoods> goodsList = indexMapper.queryGoodsByCategoryId(categoryId);
        goodsListBean.setGoodsList(goodsList);
        //2.goods数量count
        int count = indexMapper.queryGoodsCountByCategoryId(categoryId);
        goodsListBean.setCount(count);
        //3.查询category，查询种类，去重

        goodsListBean.setFilterCategoryList(null);//不知道意义，无法查询

        return goodsListBean;
    }

    @Override
    public GoodsListBean getListByBrandId(Integer brandId, Integer page, Integer size) {

        GoodsListBean goodsListBean = new GoodsListBean();
        //1.查询goods
        List<NewGoods> goodsList = indexMapper.queryGoodsByBrandId(brandId);
        goodsListBean.setGoodsList(goodsList);
        //2.count
        int count = indexMapper.queryGoodsCountByBrandId(brandId);
        goodsListBean.setCount(count);
        //3.FilterCategoryList
        goodsListBean.setFilterCategoryList(null);

        return goodsListBean;
    }

    /*
    获取货物信息并在用户登录的情况下记录用户访问的商品信息以及访问时间等相关信息
     */
    @Override
    public GoodsDetailBean getDetail(Integer id) {

        GoodsDetailBean goodsDetailBean = new GoodsDetailBean();
        //1.封装规格表
        List<GoodsSpec> goodsSpecs0 = indexMapper.querySpecificationById(id,styleSpecification);
        List<GoodsSpec> goodsSpecs1 = indexMapper.querySpecificationById(id,colorSpecification);
        List<SpecificationBean> specBeans = new ArrayList<>();
        if (goodsSpecs0.size() > 0){
            //name,List<GoodsSpec>
            specBeans.add(new SpecificationBean(goodsSpecs0.get(0).getSpecification(),goodsSpecs0));
        }
        if (goodsSpecs1.size() > 0){
            specBeans.add(new SpecificationBean(goodsSpecs1.get(0).getSpecification(),goodsSpecs1));
        }

        goodsDetailBean.setSpecificationList(specBeans);
        //2.团购
        GrouponIndex grouponIndex = null;
        goodsDetailBean.setGroupon(grouponIndex);
        //3.常见问题
        List<Issue> issues = indexMapper.queryIssue();
        goodsDetailBean.setIssue(issues);
        //4.用户收藏
        Integer num = indexMapper.queryCollectNum(id);
        goodsDetailBean.setUserHasCollect(num);
        //5.shareImage
        String shareImage = indexMapper.queryShareImageByGoodsId(id);
        goodsDetailBean.setShareImage(shareImage);
        //6.评论
        List<CommentBeanSon> commentBeanSons = indexMapper.queryCommentByGoodsId(id);
        CommentBean commentBean = new CommentBean(commentBeanSons,commentBeanSons.size());
        goodsDetailBean.setComment(commentBean);
        //7.Attribute
        List<GoodsAttribute> attributes = indexMapper.queryAttributeByGoodsId(id);
        goodsDetailBean.setAttribute(attributes);
        //8.brand
        Brand brand = indexMapper.queryDetailBrandByGoodsId(id);
        goodsDetailBean.setBrand(brand);
        //9.
        List<GoodsProduct> products = indexMapper.queryProductBiGoodsId(id);
        goodsDetailBean.setProductList(products);
        //10
        Goods goods = indexMapper.queryAllGoodsInfoByGoodsId(id);
        goodsDetailBean.setInfo(goods);
        //11.增加到足迹表中
        Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute("id");
        if (userId != null){
            FootPrint footPrint = new FootPrint();
            footPrint.setUserId(userId);
            footPrint.setGoodsId(id);
            footPrint.setAddTime(TimeTool.getCurrentTime());
            footPrint.setUpdateTime(TimeTool.getCurrentTime());
            footPrint.setDeleted(false);
            footPrintMapper.insert(footPrint);
        }



        return goodsDetailBean;
    }

    public List<NewGoods> getRelated(Integer id){
        List<NewGoods> goodsList = indexMapper.queryRelatedGoods(id);
        return goodsList;
    }

    @Override
    public GoodsListBean getListByKeyword(String keyword, String sort, String order) {

        GoodsListBean goodsListBean = new GoodsListBean();
        //1.info
        List<NewGoods> goodsList = indexMapper.queryGoodsByKeyword(keyword, sort, order);
        goodsListBean.setGoodsList(goodsList);
        //2.count
        int count = indexMapper.queryGoodsCountByKeyword(keyword);
        goodsListBean.setCount(count);
        //3.
        goodsListBean.setFilterCategoryList(null);

        //搜索记录存入数据库
        int  id = (int)SecurityUtils.getSubject().getSession().getAttribute("id");

        indexMapper.insertHistory
                (id,keyword,"wx",TimeTool.getCurrentTime(),TimeTool.getCurrentTime(),0);
        return goodsListBean;
    }

}
